﻿using GalaSoft.MvvmLight;
using Glorysoft.Auto.Xfw;
using Glorysoft.EAP.Core.Enums;
using Glorysoft.EAP.EAPClient.Common;
using Serilog.Core;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading;

namespace Glorysoft.EAP.EAPClient.Model
{
    public class RunService : ViewModelBase
    {

        #region 单例模式
        private static readonly Lazy<RunService> Lazy = new Lazy<RunService>(() => new RunService());
        public static RunService Current
        {
            get
            {
                return Lazy.Value;
            }
        }
        #endregion
        private RunService()
        {
        }

        private static Logger ServiceLogger = XfwContext.EapLogger;
        public bool ListenBarcode = false;
        private bool connStatus;
        public bool ConnStatus
        {
            get
            {
                return connStatus;
            }
            set
            {
                if (value)
                {
                    BGround = "Green";
                    ConnText = " online";
                }
                else
                {
                    BGround = "Red";
                    ConnText = "offline";
                }
                connStatus = value;
            }
        }
        private bool eQPconnStatus;
        public bool EQPConnStatus
        {
            get
            {
                return eQPconnStatus;
            }
            set
            {
                if (value)
                {
                    EQPBGround = "Green";
                }
                else
                {
                    EQPBGround = "Red";
                }
                eQPconnStatus = value;
            }
        }
        private string bGround;
        public string BGround
        {
            get { return bGround; }
            set { bGround = value; RaisePropertyChanged("BGround"); }
        }
        private string eQPbGround;
        public string EQPBGround
        {
            get { return eQPbGround; }
            set { eQPbGround = value; RaisePropertyChanged("EQPBGround"); }
        }
        private string proberbGround;
        public string ProberBGround
        {
            get { return proberbGround; }
            set { proberbGround = value; RaisePropertyChanged("ProberBGround"); }
        }
        private string testerbGround;
        public string TesterBGround
        {
            get { return testerbGround; }
            set { testerbGround = value; RaisePropertyChanged("TesterBGround"); }
        }

        private string connText;
        public string ConnText
        {
            get { return connText; }
            set { connText = value; RaisePropertyChanged("ConnText"); }
        }
        public bool IsLoginOpen;
        private string buttonName = "LOGIN";
        public string ButtonName
        {
            get { return buttonName; }
            set
            {
                buttonName = value;
                RaisePropertyChanged("ButtonName");
            }
        }
        private string name="PLEASE LOGIN";
        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                RaisePropertyChanged("Name");
            }
        }
        private string lisenBtnContext;
        public string LisenBtnContext
        {
            get { return lisenBtnContext; }
            set
            {
                lisenBtnContext = value;

                RaisePropertyChanged("LisenBtnContext");
            }
        }
        private string lbl_CarrierID;
        public string Lbl_CarrierID
        {
            get { return lbl_CarrierID; }
            set
            {
                lbl_CarrierID = value;

                RaisePropertyChanged("Lbl_Lotid");
            }
        }
        private string trackOutBtn;
        public string TrackOutBtn
        {
            get { return trackOutBtn; }
            set
            {
                trackOutBtn = value;

                RaisePropertyChanged("TrackOutBtn");
            }
        }

        private string _RefreshName = "&#xf146;  刷新";
        public string RefreshName
        {
            get { return _RefreshName; }
            set
            {
                _RefreshName = value;
                RaisePropertyChanged("RefreshName");
            }
        }
        private bool _RefreshStatus = true;
        public bool RefreshStatus
        {
            get { return _RefreshStatus; }
            set
            {
                _RefreshStatus = value;
                RaisePropertyChanged("RefreshStatus");
            }
        }
        private string _RGYColor = Consts.YELLOW;
        public string RGYColor
        {
            get
            {
                return _RGYColor;
            }
            set
            {
                _RGYColor = value;
                RaisePropertyChanged("RGYColor");
            }
        }
        private EqpClientInfo eQPInfo;
        public EqpClientInfo EQPInfo
        {
            get
            {
                return eQPInfo;
            }
            set
            {
                eQPInfo = value;
                RaisePropertyChanged("EQPInfo");
            }
        }
        private ObservableCollection<EqpClientInfo> equipmentList;
        public ObservableCollection<EqpClientInfo> EquipmentList
        {
            get { return equipmentList; }
            set
            {
                equipmentList = value;
            }
        }
        /// <summary>
        /// 实时扫描信息
        /// </summary>
        private ScanModel scanModelInfo = new ScanModel();
        public ScanModel ScanModelInfo
        {
            get
            {
                return scanModelInfo;
            }
            set
            {
                scanModelInfo = value;
                RaisePropertyChanged("ScanModelInfo");
            }
        }
        private string clientLanguage = "EN";
        public string ClientLanguage
        {
            get { return clientLanguage; }
            set
            {
                if (value == clientLanguage) return;
                clientLanguage = value;
                RaisePropertyChanged("ClientLanguage");
            }
        }
        private string languageBtnTxt = "En";
        public string LanguageBtnTxt
        {
            get { return languageBtnTxt; }
            set
            {
                if (value == languageBtnTxt) return;
                languageBtnTxt = value;
                RaisePropertyChanged("LanguageBtnTxt");
            }
        }

        public Dictionary<string, Timer> MidConnTimers = new Dictionary<string, Timer>();//连接用于计时（）
        public Dictionary<string, bool> MIDStatus = new Dictionary<string, bool>();
        public delegate void MessageNotifyHandler(MessageOrigin origin, MessageType type, string message);
        public delegate void RGYHandler(string color);
        public event MessageNotifyHandler MsgNotifier;
        public event RGYHandler RGYHandl;

        public void LogMsg(MessageOrigin origin, MessageType type, string message)
        {
         
            if (MsgNotifier != null)
                MsgNotifier(origin, type, message);
        }
        public void RGYChanged(string color)
        {
            if (RGYHandl != null)
                RGYHandl(color);
        }
        private object lockObj = new object();

        public void SetMidConn(object state)
        {
            lock (MIDStatus)
            {
                string tagkey = state.ToString();
                if (MIDStatus.Count > 0
                    && MIDStatus.ContainsKey(tagkey)
                    && MIDStatus[tagkey] == true)
                {
                    if (Current.EquipmentList.Count > 0)
                    {
                        foreach (EqpClientInfo entity in Current.EquipmentList)
                        {
                            if (tagkey == entity.EqpName)
                            {
                                entity.ConnStatus = false;
                                entity.EQPConnStatus = false;
                                MIDStatus[tagkey] = false;
                                break;
                            }
                        }
                    }
                    MidConnTimers[tagkey].Dispose();
                    MidConnTimers[tagkey] = null;
                }
            }
        }
        public bool VMFilg = true;

        public KVMInfo KvmInfo { get; set; } = new KVMInfo();
    }
}
